#!/bin/sh
#
# Run configure like it is run for current system ...
# the goal is to make the workarea files
# src/include/pcp.conf and src/include/builddefs match /etc/pcp.conf
# and /usr/include/pcp/builddefs so "sudo make install"
# works without surprises from anywhere in the source tree and
# "make setup" works in the qa directory
#

_usage()
{
    echo >&2 "Usage: $0 [options]"
    echo >&2 "Options:"
    echo >&2 "-q	do configure, skip make and check steps"
    exit 1
}

quick=false
while getopts 'q?' p
do
    case "$p"
    in
	q)	quick=true
		refresh=false
		;;
	?)	_usage
		# NOTREACHED
    esac
done
shift `expr $OPTIND - 1`
[ $# -ne 0 ] && _usage

if [ ! -f VERSION.pcp ]
then
    echo "Arrgh: VERSION.pcp not found, you need to be at the root of the git tree"
    exit 1
fi

. VERSION.pcp
version=$PACKAGE_MAJOR.$PACKAGE_MINOR.$PACKAGE_REVISION

target=`uname -s | sed -e 's/ .*//' | tr 'A-Z' 'a-z'`

# Note:	this logic needs to match the settings in Makepkgs at the
# 	top of thes source tree
#
dorpm=unknown
dodeb=unknown

if [ $dorpm = unknown ]
then
    dorpm=false
    [ -x /usr/bin/rpmbuild ] && dorpm=true
fi
if [ $dodeb = unknown ]
then
    dodeb=false
    [ -x /usr/bin/dpkg-buildpackage ] && dodeb=true
fi

# Hmm ...
#
if $dodeb && $dorpm
then
    echo "Do not know how to build RPM and Debian packages at the same time!"
    exit 1
fi

if $dodeb
then
    # needs to match configure_paths in debian/rules ... so just get the
    # magic setting from there
    #
    if [ ! -f debian/rules ]
    then
	echo "Botch: cannot find debian/rules"
	exit 1
    fi
    configopts=`sed -n <debian/rules -e '/^configure_paths/s/^[^=]*= *//p'`
    if [ -z "$configopts" ]
    then
	echo "Botch: cannot get configopts from configure_paths in debian/rules"
	exit 1
    fi
    # and optionally configure_tools from debian/rules also ...
    #
    configtools=`sed -n <debian/rules -e '/^configure_tools/s/^[^=]*= *//p'`
    echo configtools=$configtools
elif $dorpm
then
    configopts=`rpmbuild --eval '--prefix=%{_prefix} --exec-prefix=%{_exec_prefix} --bindir=%{_bindir} --sbindir=%{_sbindir} --sysconfdir=%{_sysconfdir} --datadir=%{_datadir} --includedir=%{_includedir} --libdir=%{_libdir} --libexecdir=%{_libexecdir} --localstatedir=%{_localstatedir} --sharedstatedir=%{_sharedstatedir} --mandir=%{_mandir}' 2>/dev/null`
    # rpmbuild clears the environment, so force these settings into the
    # rpm build configure
    #
    configopts="$configopts --with-make=$MAKE --with-tar=$TAR --with-zip=$ZIP"
elif [ $target = darwin ]
then
    # On Mac OS X platforms, we're using brew ...
    #
    export CC=clang
    export CXX=clang
    configopts="`brew diy --version=$version --name=pcp`"
elif [ $target = netbsd ]
then
    # Try to mimic where pkgsrc/pkgin place things
    #
    configopts="--prefix=/usr/pkg --exec-prefix=/usr/pkg --mandir=/usr/pkg/man --with-rcdir=/etc/rc.d --localstatedir=/usr/pkg --with-rundir=/var/run --with-tmpdir=/var/tmp --with-logdir=/var/log/pcp"
elif [ $target = freebsd ]
then
    # Python build does not work on FreeBSD 9.3, so skip it
    #
    case `uname -r`
    in
	9.3*)
		# default case, just without python
		#
		configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-python=no --with-python3=no"
		;;
	*)
		# default case, as below ...
		#
		configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var"
		;;
    esac
elif [ $target = sunos ]
then
    # Need to take control of the Python versions, otherwise standard.
    #
    configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var --with-python=no --with-python3=check"
else
    # default case, we know no better
    #
    configopts="--prefix=/usr --sysconfdir=/etc --localstatedir=/var"
fi

case "$target"
in
    mingw)
	configopts="$configopts --disable-ssp --without-threads"
	;;
    linux)
	ARCH=`uname -m`
	[ -f /etc/slackware-version -a "$ARCH" = x86_64 ] && configopts="$configopts --libdir=/usr/lib64"
	if `which dpkg >/dev/null 2>&1`
	then
	    configopts="$configopts --libexecdir=/usr/lib"
	fi
	;;
    sunos|SunOS|solaris)
	ARCH=`isainfo -k`
	[ "$ARCH" = amd64 -o "$ARCH" = sparcv9 ] && configopts="$configopts --libdir=/usr/lib/64"
	;;
esac

# now some specific QA/development hosts with special needs ...
#
case `hostname`
in
    vm11|vm11.localdomain)
	# gcc -O2 is broken here (for the linux PMDA)
	#
	configopts="$configopts --without-optimization"
	;;
    bozo-vm|bozo-vm.localdomain)
	# trying to build pmview here ...
	#
	configopts="$configopts --with-qt=debug --without-optimization"
	;;
esac

echo "Using: $configtools configure $configopts ..."

eval $configtools ./configure $configopts

if [ ! -f src/include/pcp.conf ]
then
    echo "Arrgh, src/include/pcp.conf not found"
    exit 1
fi

. src/include/pcp.conf

here=`pwd`

# build critical stuff for "make setup" in qa directory
#
cd src/include; $PCP_MAKE_PROG; cd $here
cd src/pmns; $PCP_MAKE_PROG; cd $here

if $quick
then
    echo "== Configured pcp ($configopts)"
else
    # build some useful libs ...
    #
    cd src/libpcp; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_static; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_pmda; $PCP_MAKE_PROG; cd $here
    cd src/libpcp_qmc; $PCP_MAKE_PROG; cd $here
    cd src/pmcpp; $PCP_MAKE_PROG; cd $here
    cd src/newhelp; $PCP_MAKE_PROG; cd $here
fi

# check expected files are the same
#

tmp=/var/tmp/$$
trap "rm -rf $tmp.* $tmp; exit 0" 0 1 2 3 15

eval `grep PCP_INC_DIR= src/include/pcp.conf`

mkdir $tmp

cat <<End-of-File | while read wa_file pkg_file
src/include/pcp.conf		/etc/pcp.conf
src/include/builddefs		$PCP_INC_DIR/builddefs
src/include/pcp/config.h	$PCP_INC_DIR/config.h
src/include/pcp/configsz.h	$PCP_INC_DIR/configsz.h
src/include/pcp/platform_defs.h	$PCP_INC_DIR/platform_defs.h
src/include/pcp/platformsz.h	$PCP_INC_DIR/platformsz.h
End-of-File
do
    if [ -f "$wa_file" ]
    then
	mkdir -p `dirname "$tmp/$wa_file"`
	# no rewrite for workarea file
	cp "$wa_file" "$tmp/$wa_file"
	if [ -f "$pkg_file" ]
	then
	    mkdir -p `dirname "$tmp/$pkg_file"`
	    # for package installed file, there are some known and
	    # expected differences from workarea pathnames (relative to
	    # $(TOPDIR)) to absolute pathnames ... reverse this so we
	    # don't get false hits in the diff(1) output
	    #
	    sed <"$pkg_file" >"$tmp/$pkg_file" \
		-e 's@$(PCP_DIR)/etc/pcp.conf@$(TOPDIR)/src/include/pcp.conf@' \
		-e 's@$(PCP_BINADM_DIR)/install-sh@$(TOPDIR)/install-sh@' \
		-e 's@$(PCP_BIN_DIR)/genpmda@$(TOPDIR)/src/genpmda/genpmda@' \
		-e 's@$(PCP_INC_DIR)/buildrules@$(TOPDIR)/src/include/buildrules@' \
		-e '/PACKAGE_BUILD ?=/s/[0-9][0-9]*/1/'
	    # end
	    ( cd $tmp; diff -uw "./$wa_file" "./$pkg_file" ) >$tmp.out
	    if [ $? -eq 0 ]
	    then
		echo "Info: $pkg_file: matches configured workarea file"
	    else
		# different, strip ./ from start of file names in diff output
		sed <$tmp.out \
		    -e '/^---/s@ ./@ @' \
		    -e '/^+++/s@ ./@ @' \
		# end
		echo "Warning: $pkg_file: not the same as $wa_file"
	    fi
	else
	    echo "Warning: $pkg_file: not found, diff skipped"
	fi
    else
	echo "Error: $wa_file: missing!"
    fi
done

echo "== Configured pcp ($configopts)"
